package leetcode_字符串._06字符的统计;

import java.util.ArrayList;

/**
 * 计数二进制子串
 */
public class _696 {

    public int countBinarySubstrings_perfect(String s) {
        int ans = 0, i = 0, last = 0;
        while (i < s.length()) {
            char c = s.charAt(i);
            int count = 0;
            while (i < s.length() && s.charAt(i) == c) {
                ++count;
                ++i;
            }
            ans += Math.min(last, count);
            last = count;
        }
        return ans;
    }

    public int countBinarySubstrings(String s) {
        int ans = 0, i = 0;
        ArrayList<Integer> list = new ArrayList<>();
        while (i < s.length()) {
            char c = s.charAt(i);
            int count = 0;
            while (i < s.length() && s.charAt(i) == c) {
                ++count;
                ++i;
            }
            list.add(count);
        }
        for (int j = 0; j < list.size() - 1; j++) {
            ans += Math.min(list.get(j), list.get(j + 1));
        }
        return ans;
    }
}
